Functions
Functions of a Single Variable
A function describes the relationship between variables.
Examples:
Details
Functions are commonly used in statistical applications, to describe relationships.
A function describes the relationship between variables. A variable is described as a function of a variable by completely specifying how can be computed for any given value of .
An example could be the relationship between a dose level and the response to the dose.
The relationship is commonly expressed by writing either or .
Usually names are given to functions, i.e. to the relationship itself. For example, might be the function and could be its value for a given number . Typically is a number but is the function, but the sloppy phrase "the function " is also common.
Examples
or specifies that the computed value of should always be , for any given value of .
Functions in R
A function can be defined in R using the function
command:
Ranges and Plots in R
Functions in R can commonly accept a range of values and will return a corresponding vector with the outcome.
Examples
f <- function(x) {return(x*12)}
x <- seq (-5,5,0,1)
y <- f(x)
plot {(x,y) type= 'l'}
Plotting Functions
In statistics, the function of interest is commonly called the response function. If we write , the outcome is usually called the response variable and is the explanatory variable. Function values are plotted on vertical axis while values are plotted on horizontal axis. This plots against .
Examples
The following R commands can be used to generate a plot for function :
x <- seq(0:10)
g <- function(x) {
+ yhat <- 2+3*x
+ return(yhat)
+ }
x <- seq(0,10,0.1)
y <- g(x)
plot(x,y,type="l", xlab="x",ylab="y")